package com.sskj.market.ui.activity;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bulong.rudeness.RudenessScreenHelper;
import com.bumptech.glide.Glide;
import com.gyf.barlibrary.ImmersionBar;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.adapter.BaseAdapter;
import com.sskj.lib.adapter.ViewHolder;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.market.R;
import com.sskj.market.R2;
import com.sskj.market.bean.ShopSearchBean;
import com.sskj.market.http.HttpConfig;
import com.sskj.market.presenter.SearchPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Flowable;

/**
 * 商城搜索
 *
 * @author Hey
 * Create at  2019/07/25
 */
@Route(path = RConfig.MARKET_ACTIVITY_SEARCH)
public class SearchActivity extends BaseActivity<SearchPresenter> {

    @BindView(R2.id.cancel_tv)
    TextView cancelTv;
    @BindView(R2.id.search_edt)
    EditText searchEdt;
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;
    @Autowired
    int category_id;
    private MVCCoolHelper<List<ShopSearchBean.DataBean>> mvcHelper;

    @Override
    public int getLayoutId() {
        return R.layout.market_activity_search;
    }

    @Override
    public SearchPresenter getPresenter() {
        return new SearchPresenter();
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void initView() {
        ARouter.getInstance().inject(this);
        ImmersionBar.with(this).fitsSystemWindows(true).statusBarColor(R.color.libSuper_content_bg).init();
        Drawable drawable = getResources().getDrawable(R.mipmap.market_icon_sousuo);
        drawable.setBounds(0, 0, 34, 34);
        ClickUtil.click(300, cancelTv, () -> {
            finish();
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setLeftPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setRightPadding((int) RudenessScreenHelper.pt2px(App.INSTANCE, 30))
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.super_divi_split))
        );


        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.market_search_item, new SlimInjector<ShopSearchBean.DataBean>() {
            @Override
            public void onInject(ShopSearchBean.DataBean item, IViewInjector injector, List list) {
                injector.text(R.id.tvSearchTitle, item.getName())
                        .text(R.id.tvSearchMoney, item.getPrice() + "LAC")
                        .text(R.id.tvSearchNumberMoney, item.getPurchase_number() + App.INSTANCE.getString(R.string.market_shoppingActivity2))
                        .clicked(R.id.llSearchItem,v -> {
                            ARouter.getInstance().build(RConfig.MARKET_ACTIVITY_COMMODITY_DETAILS).withInt("product_id", item.getId()).navigation();
                        })
                        .image(R.id.ivSearchItem,HttpConfig.BASE_IMG_URL + item.getPics().get(0));
            }
        }).attachTo(recyclerView).updateData(new ArrayList());

        ModelRx2DataSource<ShopSearchBean.DataBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<ShopSearchBean.DataBean>)
                page -> mPresenter.getSearchData(page + "", searchEdt.getText().toString(), category_id, true), 20);
        mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        //点击搜索进行显示
        searchEdt.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                // 先隐藏键盘
                ((InputMethodManager) searchEdt.getContext()
                        .getSystemService(Context.INPUT_METHOD_SERVICE))
                        .hideSoftInputFromWindow(SearchActivity.this
                                        .getCurrentFocus().getWindowToken(),
                                InputMethodManager.HIDE_NOT_ALWAYS);
                if (searchEdt.getText().toString().equals("")){
                    ToastUtil.showShort(App.INSTANCE.getText(R.string.market_search_toast));
                }else{
                    mvcHelper.refresh();
                }
                return true;
            }
            return false;
        });
    }

    @Override
    public void initData() {

    }
}
